package com.hc.config.redis.service;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Component
public class RedisService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;

    /**
     * 不带过期时间的设置
     * @param key
     * @param value
     */
    public  void  set(String key,String value){
        stringRedisTemplate.opsForValue().set(key,value);
    }

    /**
     * 带过期时间的设置
     * @param key
     * @param value
     * @param timeOut  过期时间
     */
    public  void  set(String key,String value,long timeOut){
        stringRedisTemplate.opsForValue().set(key,value,timeOut, TimeUnit.SECONDS);
    }

    /**
     * 获取值
     * @param key
     * @return
     */
    public String get(String key){
        return stringRedisTemplate.opsForValue().get(key);
    }


    /**
     * 清除缓存
     * @param key
     */
    public void  del(String key){
        stringRedisTemplate.delete(key);
    }

    /**
     * 对象存储
     */
    public  void  set(String key,Object value,long timeOut){
        redisTemplate.opsForValue().set(key,value,timeOut, TimeUnit.SECONDS);
    }
}
